import store from '@/store';
import {BtnType, Dialog} from '@/components/dialog';
import {EntityTypes} from '@/utils/orgConstants';
import {doCheckScope as processCheckScopeApi} from '@/api/modules/SelectOrgApi';

/**
 * 对数据进行校验职务级别等待
 * @param entity 需要检查的实体
 * @param context 校验数据上下文
 * @returns {Promise<boolean>}
 */
const doCheckEntity = async (entity, context = { notIncludeSubDept: [], currentPanel: '' }) => {
	return new Promise(async resolve => {
		let selectParams = store.state.selectData.selectParams;
		console.debug('doCheckEntity == > entity', entity, context);
		if (!selectParams.selectType.includes(EntityTypes.Member)) {
			// 不能选择人员不进行职级校验
			resolve(true);
			return;
		}
		const { checkConfigMap, excludeBeyondMemberSizeMap, isNeedCheckLevelScope, isNotShowNoMemberConfirm, unallowedSelectEmptyGroup } = selectParams;

		let checkConfig = checkConfigMap[entity.type];
		if (!checkConfig) {
			// 后端没有返回对应类型配置标示不检查
			resolve(true);
			return;
		}

		let { checkWorkScope, checkLevelScope, checkEmptyMember, checkMemberSize } = checkConfig;
		let command = [];
		let argument = {};
		let entityId = entity.id;
		if (entity.type === EntityTypes.Department && context.notIncludeSubDept && context.notIncludeSubDept.includes(entityId)) {
			// 不包含子部门
			entityId = `${entityId}|1`;
		}
		// 检查工作范围
		if (isNeedCheckLevelScope && checkWorkScope) {
			command.push('checkBeyondWorkScope');
			argument.isNeedCheckLevelScope = selectParams.isNeedCheckLevelScope;
			argument.showAllOuterDepartment = selectParams.showAllOuterDepartment;
			// checkOK = await doCheckWorkScope(selectParams, entity, context);
		}
		// 检查职务级别可见性
		if (isNeedCheckLevelScope && checkLevelScope) {
			command.push('checkBeyondLevelScope');
			argument.isNeedCheckLevelScope = selectParams.isNeedCheckLevelScope;
			argument.showAllOuterDepartment = selectParams.showAllOuterDepartment;
		}
		// 检查是否为空实体
		if (checkEmptyMember && (unallowedSelectEmptyGroup || (!unallowedSelectEmptyGroup && !isNotShowNoMemberConfirm))) {
			command.push('checkEmptyMember');
			argument.accessFileSecretLevel = selectParams.accessFileSecretLevel;
			argument.showConcurrentMember = selectParams.showConcurrentMember;
			argument.isNotShowNoMemberConfirm = selectParams.isNotShowNoMemberConfirm;
			argument.unallowedSelectEmptyGroup = selectParams.unallowedSelectEmptyGroup;
			argument.isNeedCheckLevelScope = selectParams.isNeedCheckLevelScope;
		}
		// 检查是否超过人数
		if (checkMemberSize) {
			command.push('checkBeyondMemberSize');
			argument.showConcurrentMember = selectParams.showConcurrentMember;
			argument.excludeElementsBeyondMemberSize = selectParams.excludeElementsBeyondMemberSize;
			argument.showAllOuterDepartment = selectParams.showAllOuterDepartment;
			argument.isShowRelatemember = selectParams.isShowRelatemember;
			argument.isNeedCheckLevelScope = selectParams.isNeedCheckLevelScope;
		}

		if (!command.length) {
			resolve(true);
			return;
		}

		let params = {
			panel: context.currentPanel,
			command: command.join(','),
			id: entityId,
			entityType: entity.type,
			arguments: argument
		};
		let checkResult = await processCheckScopeApi(params);
		// 检查出错
		if (Number(checkResult.code) !== 0) {
			Vue.prototype.$message.error(checkResult.message);
			resolve(false);
			return;
		}

		let checkBeyondWorkScopeR = checkResult.data.checkBeyondWorkScope;
		if (checkBeyondWorkScopeR && !checkBeyondWorkScopeR.success) {
			await Dialog.alert(checkBeyondWorkScopeR.msg);
			resolve(false);
			return;
		}
		let checkBeyondLevelScopeR = checkResult.data.checkBeyondLevelScope;
		if (checkBeyondLevelScopeR && !checkBeyondLevelScopeR.success) {
			await Dialog.alert(checkBeyondLevelScopeR.msg);
			resolve(false);
			return;
		}
		let checkEmptyMemberR = checkResult.data.checkEmptyMember;
		if (checkEmptyMemberR && !checkEmptyMemberR.success) {
			// 人员为空
			if (unallowedSelectEmptyGroup) {
				await Dialog.alert($i18n('selectPeople.alertEmptyMemberNO',entity.name));
				resolve(false);
				return;
			}
			if (!isNotShowNoMemberConfirm) {
				// isNotShowNoMemberConfirm : 是否不显示“xxx部门下无人，是否继续选择”的提示语，默认false，即显示。
				const btn = await Dialog.confirm($i18n('selectPeople.alertEmptyMember',entity.name));
				resolve(btn === BtnType.OK);
				return;
			}
		}
		let checkBeyondMemberSizeR = checkResult.data.checkBeyondMemberSize;
		if (checkBeyondMemberSizeR && !checkBeyondMemberSizeR.success) {
			await Dialog.alert(checkBeyondMemberSizeR.msg);
			resolve(false);
			return;
		}
		resolve(true);
	});
};


export const CheckUtils = {
	doCheckEntity
};
